package com.qp.basics.estivate.transaction.support;

import java.util.HashMap;
import java.util.Map;

public class TransactionSynchronizationManager {

	private static final ThreadLocal resources = new ThreadLocal();

	public static Object getResource(Object actualKey) {
		Map map = (Map) resources.get();
		if (map == null) {
			return null;
		}
		Object value = map.get(actualKey);
		return value;
	}

	public static void bindResource(Object key, Object value) {
		Map map = (Map) resources.get();
		// set ThreadLocal Map if none found
		if (map == null) {
			map = new HashMap();
			resources.set(map);
		}
		map.put(key, value);

	}

	public static Object unbindResource(Object key)
			throws IllegalStateException {
		Map map = (Map) resources.get();
		if (map == null) {
			return null;
		}
		Object value = map.remove(key);
		// Remove entire ThreadLocal if empty...
		if (map.isEmpty()) {
			resources.set(null);
		}
		return value;
	}

	public static void clear() {

	}
}
